package data

import (
	"context"
	"gitee.com/shuokeyun/kratos/errors"
	"gitee.com/shuokeyun/merchant-service/internal/data/ent/merchantuser"

	"gitee.com/shuokeyun/merchant-service/internal/data/ent"
	"gitee.com/shuokeyun/merchant-service/internal/domain"
)

var _ domain.MerchantUserRepo = (*merchantUserRepo)(nil)

type merchantUserRepo struct {
	client *ent.Client
	user   *ent.MerchantUserClient
}

func NewMerchantUserRepo(client *ent.Client) domain.MerchantUserRepo {
	return &merchantUserRepo{
		client: client,
		user:   client.MerchantUser,
	}
}

func (m *merchantUserRepo) ListMerchantUser(ctx context.Context, search *domain.SearchMerchantUserList) ([]*domain.MerchantUser, int32, error) {
	//TODO implement me
	panic("implement me")
}

func (m *merchantUserRepo) GetMerchantUser(ctx context.Context, id int32, fields []string) (*domain.MerchantUser, error) {
	po, err := m.user.Query().Where(merchantuser.ID(int64(id))).Select(fields...).Only(ctx)
	if err != nil {
		if ent.IsNotFound(err) {
			return nil, errors.ErrorRecordNotFound("user %d not fund", id)
		}
	}
	return userPoConvDo(po), nil
}

func (m *merchantUserRepo) GetMerchantUserByMobile(ctx context.Context, mobile string, fields []string) (*domain.MerchantUser, error) {
	po, err := m.user.Query().Where(merchantuser.Mobile(mobile)).Select(fields...).Only(ctx)
	if err != nil {
		if ent.IsNotFound(err) {
			return nil, errors.ErrorRecordNotFound("user mobile %s not fund", mobile)
		}
		return nil, err
	}
	return userPoConvDo(po), nil
}

func (m *merchantUserRepo) CreateMerchantUser(ctx context.Context, do *domain.MerchantUser) (*domain.MerchantUser, error) {
	//TODO implement me
	panic("implement me")
}

func (m *merchantUserRepo) UpdateMerchantUser(ctx context.Context, do *domain.MerchantUser, fields []string) (*domain.MerchantUser, error) {
	//TODO implement me
	panic("implement me")
}

func (m *merchantUserRepo) DeleteMerchantUser(ctx context.Context, id int32) error {
	//TODO implement me
	panic("implement me")
}
